<template>
	<view class="box">
		<view class="card">
			<!-- 标题 -->
			<view class="title f-w">
				<view class="f30 bold" v-if="type==1">评价商品 </view>
				<view class="f30 bold" v-if="type==2">评价买手 </view>
				<view class="f30 bold" v-if="type==3">评价骑手 </view>
				<view class="f24 c999"> (评价可得积分)</view>
			</view>
			<!-- 文字评论 -->
			<textarea v-model="comment" placeholder="说说哪里满意，帮大家选择" maxlength="-1" auto-height />
			<!-- 上传 -->
			<view class="upload">
				<!-- 图片上传 -->
				<view class="img" v-for="(item,index) in imgs" :key="index">
					<view class="img2 f-w">
						<image :src="item.path" mode="" v-if="isImageType(item.path)"></image>
						<video :src="item.path" v-else></video>
					</view>
					<view class="close f-w" @click.stop="del(index)">
						<image src="https://jw.xzsw2021.com/static/client/icon_95@2x.png" mode=""></image>
					</view>
				</view>

				<view class="choosepic" @click="choosemedia">
					<image src="/static/img/icon_212@2x.png" mode=""></image>
					<text>上传图片/视频</text>
				</view>

			</view>
			<!-- 评分 -->
			<view class="rate f28">
				<view class="fw-bt" v-for="(item,key) in star_2" v-if="type == 2">
					<view class="label">
						{{item.name}}
					</view>
					<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="item.val">
					</u-rate>
				</view>
				<view class="fw-bt" v-if="type == 2">
					<view class="label">
						买手综合评分
					</view>
					<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="synthetical_star">
					</u-rate>
				</view>
				<view class="fw-bt" v-for="(item,key) in star_3" v-if="type == 3">
					<view class="label">
						{{item.name}}
					</view>
					<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="item.val">
					</u-rate>
				</view>
				<view class="fw-bt" v-if="type == 3">
					<view class="label">
						骑手综合评分
					</view>
					<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="synthetical_star">
					</u-rate>
				</view>
				<template v-if="type == 1">
					<view class="fw-bt" v-for="(item,key) in star_1" v-if="type == 1">
						<view class="label">
							{{item.name}}
						</view>
						<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="item.val">
						</u-rate>
					</view>
					<view class="fw-bt" >
						<view class="label">
							商品评分
						</view>
						<u-rate activeColor="#FC9448" size="24" gutter="10" :count="count" v-model="product_score">
						</u-rate>
					</view>
					
				</template>

			</view>
		</view>
		<!-- 分割 -->

		<!-- 提交按钮 -->
		<view class="btn fw-bt">
			<view class="f-w" @click="isShow=!isShow">
				<view class="tag4 f-w">
					<image src="https://jw.xzsw2021.com/static/order/icon_145@2x.png" mode="" v-if="!isShow"></image>
					<image src="https://jw.xzsw2021.com/static/order/icon_143@2x.png" mode="" v-else></image>
				</view>
				<view class="f28 c666">匿名</view>
			</view>
			<view class="btn1" @click="submit">立即评价</view>
		</view>
	</view>
</template>

<script>
	import Cache from '@/util/cache.js'
	import {
		userProductCommentApi
	} from '@/api/product.js'
	import {
		userOrderTaskCommentApi
	} from '@/api/task.js'

	import {
		baseUrl
	} from '@/util/api.js'
	export default {
		data() {
			return {
			
				type: 0, // 1 商品 2 买手 3 骑手
				comment: '',
				id: '', //订单id
				isShow: false, //匿名
				imgs: [],
				count: 5,
				value: 0,
				product_score: 0, //商品评分
				service_score: 0, //服务评分
				star_1: [{ //评价商品
						name: "菜品新鲜度",
						val: 0
					},
					{
						name: "商品包装",
						val: 0
					},
					{
						name: "菜品足量",
						val: 0
					},
					{
						name: "菜品满意度",
						val: 0
					}
				],
				star_2: [{ //评价买手
						name: "菜品新鲜度",
						val: 0
					},
					{
						name: "商品包装",
						val: 0
					},
					{
						name: "菜品足量",
						val: 0
					},
					{
						name: "菜品满意度",
						val: 0
					}
				],
				star_3: [{ //骑手
					name: '服务态度',
					val: 0
				}, {
					name: '配送速度',
					val: 0
				}],
				cacheData: {},
				order_product_id: 0, //评价商品id
				task_id: 0, //评价任务  任务id
				synthetical_star: 0,
				order_group_id: 0
			}
		},
		onLoad(options) {
			console.log(options)
			this.type = options.type
			if (this.type == 1) {
				this.order_product_id = options.order_product_id
			} else {
				this.order_group_id = options.order_group_id
				this.task_id = options.task_id
			}
		},
		methods: {
			// 删除当前图片
			del(index) {
				this.imgs.splice(index, 1);
			},
			isImageType(str) {
				var index = str.lastIndexOf('.') // 获取指定字符串最后一次出现的位置，返回index
				var str = str.substr(index + 1)
				return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].indexOf(str.toLowerCase()) !== -1
			},
			choosemedia() {
				let imgs = this.imgs
				uni.chooseMedia({
					count: 9,
					mediaType: ['image', 'video'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success: res => {
						uni.showLoading({
							title: '上传中'
						});
						res.tempFiles.forEach((item, index) => {
							uni.uploadFile({
								url: `${baseUrl}upload/image/field`, //上传图片接口地址
								filePath: item.tempFilePath,
								name: 'field',
								fileType: item.fileType,
								formData: {
									'filename': 'field'
								},
								header: {
									"X-Token": "Bearer " + Cache.get('token'),
									"Content-Type": "multipart/form-data",
								},
								success: uploadFileRes => {
									uni.hideLoading()
									imgs.push(JSON.parse(uploadFileRes.data).data);
									this.imgs = imgs

								},
								fail() {
									uni.showToast({
										title: '上传失败',
										icon: 'none'
									})
								}
							})
						})
					}
				})
			},


			// 提交评价
			// 上传
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log('aa', event, event.name);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://www.ganqi01.com/index.php/api/index/uploadPicture', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								let data = JSON.parse(res.data);
								console.log('数据', data.data.full_url);

								resolve(data.data.full_url)
							}, 1000)
						}
					});
				})
			},
			// 提交评价
			submit() {
				let image = []
				this.imgs.forEach(i => {
					image.push(i.path)
				})
				let product = { //商品评价
					product_score: this.product_score,
					comment: this.comment,
					service_score: this.service_score,
					postage_score: 5,
					pics: image
				}
				let other_star = {}
				let startData;
				
				if (this.type == 2) {
					startData = this.star_2
				} else {
					startData = this.star_3
				}
				
				if (this.type == 1) {
					startData = this.star_1
				}
				
				startData.forEach(item => {
					this.$set(other_star, item.name, item.val)
				})

				let buyer = { //评价买手
					other_star,
					task_id: this.task_id,
					synthetical_star: this.synthetical_star,
					is_anonymous: this.isShow,
					image: image,
					comment: this.comment,
				}

				let rider = { //评价骑手
					other_star,
					task_id: this.task_id,
					synthetical_star: this.synthetical_star,
					is_anonymous: this.isShow,
					image: image,
					comment: this.comment,
				}
                product.other_star=other_star
				
				
				if (this.type == 1) {
					userProductCommentApi(this.order_product_id, product).then(res => {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
						setTimeout(function(){
							uni.navigateBack({
								
							})
						},1000)

					}).catch(err => {
						uni.showToast({
							title: err.message,
							icon: "none"
						})
					})
				} else {
					let con;
					if (this.type == 2) {
						con = {
							buyer
						}
					} else {
						con = {
							rider
						}
					}
					userOrderTaskCommentApi(this.order_group_id, con).then(res => {
						console.log(res, '评价买手骑手')
						uni.showToast({
							title: res.message,
							icon: "none"
						})
						setTimeout(function(){
							uni.navigateBack({
								
							})
						},1000)
					}).catch(err => {
						uni.showToast({
							title: err.message,
							icon: "none"
						})
						console.log(err, '??????')
					})
				}



				// if (this.isShow) {
				// 	this.sendData.isShow = true
				// }
				// if (this.orign) {
				// 	uni.redirectTo({
				// 		url: '/pages/state/success/success?type=8'
				// 	})
				// } else {
				// 	uni.redirectTo({ //满汉任务-评价成功
				// 		url: '/pages/state/success/success?type=11'
				// 	})
				// }

			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		position: relative;
		flex-shrink: 0;
		width: 160rpx;
		height: 160rpx;
		background: #F8F8F8;

		.img2 {
			width: 160rpx;
			height: 160rpx;

			image,
			video {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}
		}

		.close {
			position: absolute;
			top: 0;
			right: 0;
			width: 40rpx;
			height: 30rpx;
			z-index: 5;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	// 上传
	.upload {
		display: flex;
		flex-wrap: wrap;

		view {
			margin-left: 16rpx;
			margin-bottom: 20rpx;
		}

		view:nth-of-type(4n+1) {
			margin-left: 0;
		}

		/deep/ .u-upload__button {
			margin: 0 0 8px 0;
		}
	}

	.choosepic {
		width: 180rpx;
		height: 180rpx;
		background: #ECECEC;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 20rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999;
		line-height: 34rpx;
		flex-direction: column;
	}

	.choosepic image {
		width: 40rpx;
		height: 36rpx;
		margin-bottom: 10rpx;
	}

	.box {

		padding: 0 0 150rpx;

		.card {
			padding: 45rpx 30rpx;

			textarea {
				margin: 30rpx auto;
				width: 690rpx;
				padding: 20rpx;
				font-size: 28rpx;
				line-height: 40rpx;
				min-height: 180rpx;
				background: #F8F8F8;
				border-radius: 8rpx;
				box-sizing: border-box;
			}
		}

		.bg {
			width: 750rpx;
			height: 20rpx;
			background: #F5F5F5;
		}


		//评分
		.rate {
			&>view {
				margin-top: 46rpx;
			}
		}

		.btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 750rpx;
			height: 130rpx;
			padding: 0 30rpx;
			background: #FFFFFF;
			border-top: 1rpx solid #eee;
			box-sizing: border-box;
			z-index: 21;

			.tag4 {
				margin-right: 10rpx;
				width: 30rpx;
				height: 30rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.btn1 {
				width: 460rpx;
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				color: #fff;
				font-size: 32rpx;
				background: linear-gradient(64deg, #2BCCA5, #22B591);
				box-shadow: 0rpx 4rpx 6rpx 0rpx rgba(20, 187, 146, 0.4);
			}
		}
	}
</style>
